package Vista;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import Controlador.Sistema;
import Negocio.PlanAdicional;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ModificarPlanAdicional extends javax.swing.JFrame {
	public static String TITLE = "Modificar Linea Adicional";
	/**
	* Auto-generated main method to display this JFrame
	*/
	private Sistema sistema;
	private JLabel jLblPlan;
	private JButton btnServiciosSeleccionados;
	private JButton jBtnGrabar;
	private JTextField jTxtCostoBase;
	private JLabel jLBLCostoBase;
	private JTextField jTxtPlan;
	private JButton jBtnBuscar;
	private JTextField jTxtBuscar;
	private JLabel jLblBuscar;

	public ModificarPlanAdicional(Sistema s) {
		// TODO Auto-generated constructor stub
		super(TITLE);
		sistema=s;
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jLblBuscar = new JLabel();
				getContentPane().add(jLblBuscar);
				jLblBuscar.setText("Buscar");
				jLblBuscar.setBounds(18, 22, 111, 16);
			}
			{
				jTxtBuscar = new JTextField();
				getContentPane().add(jTxtBuscar);
				jTxtBuscar.setBounds(141, 19, 121, 23);
			}
			{
				jBtnBuscar = new JButton();
				getContentPane().add(jBtnBuscar);
				jBtnBuscar.setText("Buscar");
				jBtnBuscar.setBounds(274, 19, 95, 23);
				jBtnBuscar.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						PlanAdicional aux = sistema.buscarPlanPA(Integer.parseInt(jTxtBuscar.getText()));
						if (aux!=null){
							jTxtPlan.setText(aux.getNombre());
							jTxtCostoBase.setText(String.valueOf(aux.getCostoBase()));
							jTxtBuscar.setEnabled(false);
							btnServiciosSeleccionados.setEnabled(true);
						}
					}
				});
			}
			{
				jLblPlan = new JLabel();
				getContentPane().add(jLblPlan);
				jLblPlan.setText("Nombre: ");
				jLblPlan.setBounds(18, 69, 111, 16);
			}
			{
				jTxtPlan = new JTextField();
				getContentPane().add(jTxtPlan);
				jTxtPlan.setBounds(141, 66, 121, 23);
			}
			{
				jLBLCostoBase = new JLabel();
				getContentPane().add(jLBLCostoBase);
				jLBLCostoBase.setText("Costo Base");
				jLBLCostoBase.setBounds(18, 107, 91, 16);
			}
			{
				jTxtCostoBase = new JTextField();
				getContentPane().add(jTxtCostoBase);
				jTxtCostoBase.setBounds(141, 104, 121, 23);
			}
			{
				jBtnGrabar = new JButton();
				getContentPane().add(jBtnGrabar);
				jBtnGrabar.setText("Grabar");
				jBtnGrabar.setBounds(18, 157, 132, 23);
				jBtnGrabar.addActionListener(new ActionListener() {

						public void actionPerformed(ActionEvent arg0) {
							if (controlCampos()){
								try {
										sistema.modificarPlan(Integer.parseInt(jTxtBuscar.getText()), jTxtPlan.getText(), Float.parseFloat(jTxtCostoBase.getText()));
									} catch (Exception ex) {								
									}
						}
																}});
			}
			{
				btnServiciosSeleccionados = new JButton();
				getContentPane().add(btnServiciosSeleccionados);
				btnServiciosSeleccionados.setText("Servicios Seleccionados");
				btnServiciosSeleccionados.setBounds(161, 157, 212, 23);
				btnServiciosSeleccionados.setEnabled(false);
				btnServiciosSeleccionados.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0){
						if (controlCampos()){
						try {
							
							
							ServiciosAgregados serviciosWindow = new ServiciosAgregados(sistema, sistema.buscarPlan(Integer.parseInt(jTxtBuscar.getText())));
							serviciosWindow.setLocationRelativeTo(null);
							serviciosWindow.setVisible(true);	

							
							
							
						} catch (Exception ex) {
			
						}
						}
						
					}
				});
			}
			pack();
			this.setSize(400, 236);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		private boolean controlCampos() {
			JFrame j1 = new JFrame();		
			@SuppressWarnings("unused")
			int numero;
			
			if (jTxtBuscar.getText().equals("")) {																					
				JOptionPane.showMessageDialog(j1,
						"Debe ingresar el Nro de Plan",
						"Omision",
						JOptionPane.WARNING_MESSAGE);	
				jTxtBuscar.requestFocus(true);
				return false;
			}
			if (jTxtCostoBase.getText().equals("")) {																					
				JOptionPane.showMessageDialog(j1,
						"Debe ingresar el Costo Base",
						"Omision",
						JOptionPane.WARNING_MESSAGE);	
				jTxtCostoBase.requestFocus(true);
				return false;
			}
			if (jTxtPlan.getText().equals("")) {																					
				JOptionPane.showMessageDialog(j1,
						"Debe ingresar el Nombre del Plan",
						"Omision",
						JOptionPane.WARNING_MESSAGE);	
				jTxtPlan.requestFocus(true);
				return false;
			}

			
			return true;
		}
}
